package com.voole.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;

import com.voole.entity.SysAuthority;
import com.voole.entity.SysRole;
import com.voole.entity.SysUser;

@Repository
public interface SysRoleDao {

	@Select(value="SELECT * FROM sys_role")
	List<SysRole> getList();
	
	@Select(value="SELECT sr.* FROM sys_user_role sur LEFT JOIN sys_user su ON sur.userid = su.id LEFT JOIN sys_role sr ON sur.roleid = sr.id WHERE sur.userid = ${userId}")
	List<SysRole> getListByUserId(@Param(value="userId")Integer userId);
	
	@Select(value="SELECT sr.role FROM sys_role_action sra LEFT JOIN sys_role sr ON sra.roleid = sr.id LEFT JOIN sys_action sa ON sra.actionid = sa.id WHERE sra.actionid = #{actionId}")
	List<String> getColumnRoleByActionId(@Param(value="actionId")Integer actionId);
	
	@Select(value="SELECT * FROM sys_role ORDER BY name ASC LIMIT #{start}, #{end}")
	List<SysRole> findByPage(@Param(value="start")Integer start, @Param(value="end")Integer end);

	@Select(value="SELECT count(1) FROM sys_role")
	Integer getCount();
	
	@Select(value="SELECT * FROM sys_role WHERE name=#{name} and role=#{role}")
	SysRole getSysRole(@Param(value="name")String name, @Param(value="role")String role);
	
	@Select(value="SELECT * FROM sys_role WHERE role = #{role}")
	SysRole getSysRoleByRole(String role);
	
	@Insert("INSERT INTO sys_role (`name`, `role`, `status`) VALUES(#{name}, #{role}, #{status})")  
	@Options(useGeneratedKeys = true, keyProperty = "id")
	Integer addSysRole(SysRole sysRole);
	
}